#include "rs485_1_rx_thread.h"

RS485_1_Rx_Thread::RS485_1_Rx_Thread(Serial_Port* sp)
{
    stop_flag = true;
    //(void)sp;
    this->serial_port = sp;

    connect(this->serial_port,SIGNAL(read_serial_signals()),this,SLOT(slot_rs485_1_port_rx_thread()));
}

RS485_1_Rx_Thread::~RS485_1_Rx_Thread()
{

}


void RS485_1_Rx_Thread::rs485_1_port_data_process(char *p_data,unsigned int data_len)
{

}

void RS485_1_Rx_Thread::slot_rs485_1_port_rx_thread()
{
    //qDebug() << "serial_port_rx_thread";
    {
        QMutexLocker locker(&stop_mutex);
        stop_flag = false;
    }

    unsigned int data_len = 0;
    char data_buff[1024];

    while(1)
    {
        {
            QMutexLocker locker(&stop_mutex);
            if(stop_flag)
            return;
        }
        memset(data_buff,0x00,sizeof(data_buff));

        data_len = serial_port->serial_port_rx_queue->circlebuff_read_packet_data(data_buff);

        if(data_len > 0)
        {
            rs485_1_port_data_process(data_buff,data_len);
        }
        else {
            stop_thread();
        }
    }
}

void RS485_1_Rx_Thread::stop_thread()
{
    {
        QMutexLocker locker(&stop_mutex);
        stop_flag = true;
    }
}
